﻿///////////////////////////////////////////////////////////////////////////////
// Activity: program.activity0
// DriveTestDiagram service implementation
///////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml;

using ccr = Microsoft.Ccr.Core;
using dss = Microsoft.Dss.Core;
using dssa = Microsoft.Dss.Core.Attributes;
using dssh = Microsoft.Dss.Core.DsspHttp;
using dssm = Microsoft.Dss.ServiceModel.DsspServiceBase;
using dssp = Microsoft.Dss.ServiceModel.Dssp;
using soap = W3C.Soap;

using submgr = Microsoft.Dss.Services.SubscriptionManager;
using drive = Microsoft.Robotics.Services.Drive.Proxy;
using log = Microsoft.Robotics.Services.Sample.Log.Proxy;
using contactsensor = Microsoft.Robotics.Services.ContactSensor.Proxy;

namespace Robotics.MotorTest.DriveTestDiagram
{
    [DisplayName("MotorTest DriveTestDiagram")]
    [Description("A user defined activity.")]
    [dssa.Contract(Contract.Identifier)]
    public class DriveTestDiagramService : dssm.DsspServiceBase
    {
        // Service state
        [dssa.InitialStatePartner(Optional = true)]
        private DriveTestDiagramState _state;

        // Service operations port
        [dssa.ServicePort("/MotorTest/DriveTestDiagram", AllowMultipleInstances = true)]
        private DriveTestDiagramOperations _mainPort = new DriveTestDiagramOperations();

        #region Partner services

        [dssa.Partner("SubMgr", Contract = submgr.Contract.Identifier, CreationPolicy = dssa.PartnerCreationPolicy.CreateAlways)]
        private submgr.SubscriptionManagerPort _subMgr = new submgr.SubscriptionManagerPort();

        // Partner: Log, Contract: http://schemas.microsoft.com/robotics/2006/08/log.html
        [dssa.Partner("Log", Contract = log.Contract.Identifier, CreationPolicy = dssa.PartnerCreationPolicy.UsePartnerListEntry)]
        log.LogOperations _logPort = new log.LogOperations();

        // Partner: VEXBumper_1, Contract: http://schemas.microsoft.com/2006/06/contactsensor.html
        [dssa.Partner("VEXBumper_1", Contract = contactsensor.Contract.Identifier, CreationPolicy = dssa.PartnerCreationPolicy.UsePartnerListEntry)]
        contactsensor.ContactSensorArrayOperations _vEXBumper_1Port = new contactsensor.ContactSensorArrayOperations();
        contactsensor.ContactSensorArrayOperations _vEXBumper_1Notify = new contactsensor.ContactSensorArrayOperations();

        // Partner: VEXBumper_2, Contract: http://schemas.microsoft.com/2006/06/contactsensor.html
        [dssa.Partner("VEXBumper_2", Contract = contactsensor.Contract.Identifier, CreationPolicy = dssa.PartnerCreationPolicy.UsePartnerListEntry)]
        contactsensor.ContactSensorArrayOperations _vEXBumper_2Port = new contactsensor.ContactSensorArrayOperations();
        contactsensor.ContactSensorArrayOperations _vEXBumper_2Notify = new contactsensor.ContactSensorArrayOperations();

        // Partner: VEXBumper_3, Contract: http://schemas.microsoft.com/2006/06/contactsensor.html
        [dssa.Partner("VEXBumper_3", Contract = contactsensor.Contract.Identifier, CreationPolicy = dssa.PartnerCreationPolicy.UsePartnerListEntry)]
        contactsensor.ContactSensorArrayOperations _vEXBumper_3Port = new contactsensor.ContactSensorArrayOperations();
        contactsensor.ContactSensorArrayOperations _vEXBumper_3Notify = new contactsensor.ContactSensorArrayOperations();

        // Partner: VEXBumper_4, Contract: http://schemas.microsoft.com/2006/06/contactsensor.html
        [dssa.Partner("VEXBumper_4", Contract = contactsensor.Contract.Identifier, CreationPolicy = dssa.PartnerCreationPolicy.UsePartnerListEntry)]
        contactsensor.ContactSensorArrayOperations _vEXBumper_4Port = new contactsensor.ContactSensorArrayOperations();
        contactsensor.ContactSensorArrayOperations _vEXBumper_4Notify = new contactsensor.ContactSensorArrayOperations();

        // Partner: VEXBumper_5, Contract: http://schemas.microsoft.com/2006/06/contactsensor.html
        [dssa.Partner("VEXBumper_5", Contract = contactsensor.Contract.Identifier, CreationPolicy = dssa.PartnerCreationPolicy.UsePartnerListEntry)]
        contactsensor.ContactSensorArrayOperations _vEXBumper_5Port = new contactsensor.ContactSensorArrayOperations();
        contactsensor.ContactSensorArrayOperations _vEXBumper_5Notify = new contactsensor.ContactSensorArrayOperations();

        // Partner: VEXBumper_6, Contract: http://schemas.microsoft.com/2006/06/contactsensor.html
        [dssa.Partner("VEXBumper_6", Contract = contactsensor.Contract.Identifier, CreationPolicy = dssa.PartnerCreationPolicy.UsePartnerListEntry)]
        contactsensor.ContactSensorArrayOperations _vEXBumper_6Port = new contactsensor.ContactSensorArrayOperations();
        contactsensor.ContactSensorArrayOperations _vEXBumper_6Notify = new contactsensor.ContactSensorArrayOperations();

        // Partner: VEXDrive, Contract: http://schemas.microsoft.com/robotics/2006/05/drive.html
        [dssa.Partner("VEXDrive", Contract = drive.Contract.Identifier, CreationPolicy = dssa.PartnerCreationPolicy.UsePartnerListEntry)]
        drive.DriveOperations _vEXDrivePort = new drive.DriveOperations();

        #endregion

        public DriveTestDiagramService(dssp.DsspServiceCreationPort creationPort)
            : base(creationPort)
        {
        }

        protected override void Start()
        {
            // If there was no initial state partner, then the service state will be null.
            if (_state == null)
            {
                // The state MUST be created before the service starts processing messages.
                _state = new DriveTestDiagramState();
            }

            // The rest of the start process requires the ability to wait for responses from
            // services and from the start handler (if any). So execution now proceeds in an
            // iterator function.
            SpawnIterator(DoStart);
        }

        private IEnumerator<ccr.ITask> DoStart()
        {
            soap.Fault fault = null;

            // Subscribe to partners

            yield return ccr.Arbiter.Choice(
                _vEXBumper_1Port.Subscribe(_vEXBumper_1Notify,                
                    typeof(Microsoft.Robotics.Services.ContactSensor.Proxy.Update)
                ),
                EmptyHandler,
                delegate(soap.Fault f)
                {
                    fault = f;
                }
            );

            if (fault != null)
            {
                LogError("Failed to subscribe to program.VEXBumperSwitch.VEXBumper_1", fault);
                StartFailed();
                yield break;
            }

            yield return ccr.Arbiter.Choice(
                _vEXBumper_2Port.Subscribe(_vEXBumper_2Notify,                
                    typeof(Microsoft.Robotics.Services.ContactSensor.Proxy.Update)
                ),
                EmptyHandler,
                delegate(soap.Fault f)
                {
                    fault = f;
                }
            );

            if (fault != null)
            {
                LogError("Failed to subscribe to program.VEXBumperSwitch.VEXBumper_2", fault);
                StartFailed();
                yield break;
            }

            yield return ccr.Arbiter.Choice(
                _vEXBumper_3Port.Subscribe(_vEXBumper_3Notify,                
                    typeof(Microsoft.Robotics.Services.ContactSensor.Proxy.Update)
                ),
                EmptyHandler,
                delegate(soap.Fault f)
                {
                    fault = f;
                }
            );

            if (fault != null)
            {
                LogError("Failed to subscribe to program.VEXBumperSwitch.VEXBumper_3", fault);
                StartFailed();
                yield break;
            }

            yield return ccr.Arbiter.Choice(
                _vEXBumper_4Port.Subscribe(_vEXBumper_4Notify,                
                    typeof(Microsoft.Robotics.Services.ContactSensor.Proxy.Update)
                ),
                EmptyHandler,
                delegate(soap.Fault f)
                {
                    fault = f;
                }
            );

            if (fault != null)
            {
                LogError("Failed to subscribe to program.VEXBumperSwitch.VEXBumper_4", fault);
                StartFailed();
                yield break;
            }

            yield return ccr.Arbiter.Choice(
                _vEXBumper_5Port.Subscribe(_vEXBumper_5Notify,                
                    typeof(Microsoft.Robotics.Services.ContactSensor.Proxy.Update)
                ),
                EmptyHandler,
                delegate(soap.Fault f)
                {
                    fault = f;
                }
            );

            if (fault != null)
            {
                LogError("Failed to subscribe to program.VEXBumperSwitch.VEXBumper_5", fault);
                StartFailed();
                yield break;
            }

            yield return ccr.Arbiter.Choice(
                _vEXBumper_6Port.Subscribe(_vEXBumper_6Notify,                
                    typeof(Microsoft.Robotics.Services.ContactSensor.Proxy.Update)
                ),
                EmptyHandler,
                delegate(soap.Fault f)
                {
                    fault = f;
                }
            );

            if (fault != null)
            {
                LogError("Failed to subscribe to program.VEXBumperSwitch.VEXBumper_6", fault);
                StartFailed();
                yield break;
            }

            // Start operation handlers and insert into directory service.
            StartHandlers();

            // Add notifications to the main interleave
            base.MainPortInterleave.CombineWith(
                new ccr.Interleave(
                    new ccr.ExclusiveReceiverGroup(
                    ),
                    new ccr.ConcurrentReceiverGroup(
                    )
                )
            );

            // Activate independent tasks
            Activate<ccr.ITask>(
                ccr.Arbiter.ReceiveWithIterator<Microsoft.Robotics.Services.ContactSensor.Proxy.Update>(true, _vEXBumper_1Notify, VEXBumper_1UpdateHandler),
                ccr.Arbiter.ReceiveWithIterator<Microsoft.Robotics.Services.ContactSensor.Proxy.Update>(true, _vEXBumper_2Notify, VEXBumper_2UpdateHandler),
                ccr.Arbiter.ReceiveWithIterator<Microsoft.Robotics.Services.ContactSensor.Proxy.Update>(true, _vEXBumper_3Notify, VEXBumper_3UpdateHandler),
                ccr.Arbiter.ReceiveWithIterator<Microsoft.Robotics.Services.ContactSensor.Proxy.Update>(true, _vEXBumper_4Notify, VEXBumper_4UpdateHandler),
                ccr.Arbiter.ReceiveWithIterator<Microsoft.Robotics.Services.ContactSensor.Proxy.Update>(true, _vEXBumper_5Notify, VEXBumper_5UpdateHandler),
                ccr.Arbiter.ReceiveWithIterator<Microsoft.Robotics.Services.ContactSensor.Proxy.Update>(true, _vEXBumper_6Notify, VEXBumper_6UpdateHandler)
            );

            yield break;
        }

        private void StartHandlers()
        {
            // Activate message handlers for this service and insert into the directory.
            base.Start();
        }

        #region Standard DSS message handlers

        [dssa.ServiceHandler(dssa.ServiceHandlerBehavior.Concurrent)]
        public virtual IEnumerator<ccr.ITask> GetHandler(Get get)
        {
            get.ResponsePort.Post(_state);
            yield break;
        }

        [dssa.ServiceHandler(dssa.ServiceHandlerBehavior.Concurrent)]
        public virtual IEnumerator<ccr.ITask> HttpGetHandler(dssh.HttpGet httpGet)
        {
            httpGet.ResponsePort.Post(new dssh.HttpResponseType(_state));
            yield break;
        }

        [dssa.ServiceHandler(dssa.ServiceHandlerBehavior.Exclusive)]
        public virtual IEnumerator<ccr.ITask> ReplaceHandler(Replace replace)
        {
            _state = replace.Body;

            replace.ResponsePort.Post(dssp.DefaultReplaceResponseType.Instance);
            base.SendNotification<Replace>(_subMgr, replace);

            yield break;
        }

        [dssa.ServiceHandler(dssa.ServiceHandlerBehavior.Concurrent)]
        public virtual IEnumerator<ccr.ITask> SubscribeHandler(Subscribe subscribe)
        {
            dssp.SubscribeRequestType request = subscribe.Body;

            yield return ccr.Arbiter.Choice(
                SubscribeHelper(_subMgr, request, subscribe.ResponsePort),
                delegate(ccr.SuccessResult success)
                {
                    base.SendNotificationToTarget<Replace>(request.Subscriber, _subMgr, _state);
                },
                delegate(Exception e) { }
            );
        }

        #endregion

        #region Custom message handlers

        [dssa.ServiceHandler(dssa.ServiceHandlerBehavior.Independent)]
        public virtual IEnumerator<ccr.ITask> ActionHandler(Action message)
        {
            // Empty handler. Respond with default response type.
            message.ResponsePort.Post(new ActionResponse());
            yield break;
        }

        #endregion

        #region Notification message handlers

        IEnumerator<ccr.ITask> VEXBumper_1UpdateHandler(Microsoft.Robotics.Services.ContactSensor.Proxy.Update message)
        {
            OnVEXBumper_1UpdateHandler handler = new OnVEXBumper_1UpdateHandler(this, Environment.TaskQueue);
            return handler.RunHandler(message);
        }

        IEnumerator<ccr.ITask> VEXBumper_2UpdateHandler(Microsoft.Robotics.Services.ContactSensor.Proxy.Update message)
        {
            OnVEXBumper_2UpdateHandler handler = new OnVEXBumper_2UpdateHandler(this, Environment.TaskQueue);
            return handler.RunHandler(message);
        }

        IEnumerator<ccr.ITask> VEXBumper_3UpdateHandler(Microsoft.Robotics.Services.ContactSensor.Proxy.Update message)
        {
            OnVEXBumper_3UpdateHandler handler = new OnVEXBumper_3UpdateHandler(this, Environment.TaskQueue);
            return handler.RunHandler(message);
        }

        IEnumerator<ccr.ITask> VEXBumper_4UpdateHandler(Microsoft.Robotics.Services.ContactSensor.Proxy.Update message)
        {
            OnVEXBumper_4UpdateHandler handler = new OnVEXBumper_4UpdateHandler(this, Environment.TaskQueue);
            return handler.RunHandler(message);
        }

        IEnumerator<ccr.ITask> VEXBumper_5UpdateHandler(Microsoft.Robotics.Services.ContactSensor.Proxy.Update message)
        {
            OnVEXBumper_5UpdateHandler handler = new OnVEXBumper_5UpdateHandler(this, Environment.TaskQueue);
            return handler.RunHandler(message);
        }

        IEnumerator<ccr.ITask> VEXBumper_6UpdateHandler(Microsoft.Robotics.Services.ContactSensor.Proxy.Update message)
        {
            OnVEXBumper_6UpdateHandler handler = new OnVEXBumper_6UpdateHandler(this, Environment.TaskQueue);
            return handler.RunHandler(message);
        }

        #endregion

        #region OnVEXBumper_1UpdateHandler class

        class OnVEXBumper_1UpdateHandler : HandlerBase
        {
            ///////////////////////////////////////////////////////////////////
            // if if{__use__0.snippet0.call - __use__0.snippet0.call.iftype}else{}
            // element0
            // __use__12.snippet.snippet.noop - [(__use__12.snippet.snippet.expr0 - __use__12.snippet.snippet.join)(__use__12.snippet.snippet.expr - &__use__12.snippet.snippet.join)] - __use__12.snippet.call - __use__12.snippet.call.iftype
            // element
            // __use__22.snippet0.snippet.noop - [(__use__22.snippet0.snippet.expr0 - __use__22.snippet0.snippet.join)(__use__22.snippet0.snippet.expr - &__use__22.snippet0.snippet.join)] - __use__22.snippet0.call - __use__22.snippet0.call.iftype
            ///////////////////////////////////////////////////////////////////

            public OnVEXBumper_1UpdateHandler(DriveTestDiagramService service, ccr.DispatcherQueue queue)
                : base(service, queue)
            {

                // Activate merge handlers
                Activate(ccr.Arbiter.Receive(true, _mergeAlpha, _mergeAlphaHandler));
                Activate(ccr.Arbiter.Receive(true, _mergeBeta, _mergeBetaHandler));
            }

            ///////////////////////////////////////////////////////////////////
            // if if{__use__0.snippet0.call - __use__0.snippet0.call.iftype}else{}
            ///////////////////////////////////////////////////////////////////

            public IEnumerator<ccr.ITask> RunHandler(Microsoft.Robotics.Services.ContactSensor.Proxy.Update message)
            {
                Increment();

                if (message.Body.Pressed)
                {

                    Increment();
                    Activate(
                        ccr.Arbiter.Choice(
                            VEXDrivePort.AllStop(new drive.AllStopRequest()),
                            OnAllStopRequestSuccess,
                            OnAllStopRequestFault
                        )
                    );
                }

                Decrement();

                yield return WaitUntilComplete();
            }

            void OnAllStopRequestSuccess(dssp.DefaultUpdateResponseType response)
            {

                Increment();
                _mergeAlpha.Post(response);

                Decrement();
            }

            void OnAllStopRequestFault(soap.Fault response)
            {

                Increment();
                _mergeBeta.Post(response);

                Decrement();
            }

            ccr.Port<Microsoft.Dss.ServiceModel.Dssp.DefaultUpdateResponseType> _mergeAlpha = new ccr.Port<Microsoft.Dss.ServiceModel.Dssp.DefaultUpdateResponseType>();

            ///////////////////////////////////////////////////////////////////
            // __use__12.snippet.snippet.noop - [(__use__12.snippet.snippet.expr0 - __use__12.snippet.snippet.join)(__use__12.snippet.snippet.expr - &__use__12.snippet.snippet.join)] - __use__12.snippet.call - __use__12.snippet.call.iftype
            ///////////////////////////////////////////////////////////////////

            void _mergeAlphaHandler(Microsoft.Dss.ServiceModel.Dssp.DefaultUpdateResponseType message)
            {
                log.LogInfoRequest request = new log.LogInfoRequest();
                request.Category = Stringize(message);
                request.Message = Stringize(message);
                LogPort.LogInfo(request);

                Decrement();
            }

            ccr.Port<W3C.Soap.Fault> _mergeBeta = new ccr.Port<W3C.Soap.Fault>();

            ///////////////////////////////////////////////////////////////////
            // __use__22.snippet0.snippet.noop - [(__use__22.snippet0.snippet.expr0 - __use__22.snippet0.snippet.join)(__use__22.snippet0.snippet.expr - &__use__22.snippet0.snippet.join)] - __use__22.snippet0.call - __use__22.snippet0.call.iftype
            ///////////////////////////////////////////////////////////////////

            void _mergeBetaHandler(W3C.Soap.Fault message)
            {
                log.LogInfoRequest request = new log.LogInfoRequest();
                request.Category = Stringize(message.Code);
                request.Message = message.Reason[0].Value;
                LogPort.LogInfo(request);

                Decrement();
            }

            class JoinAlpha
            {
                public string Message;
                public string Category;

                public JoinAlpha()
                {
                }

                public JoinAlpha(object[] args)
                {
                    Message = args[0].ToString();
                    Category = args[1].ToString();
                }
            }

            ccr.Port<object>[] _joinAlphaPorts = new ccr.Port<object>[2]{
                new ccr.Port<object>(),
                new ccr.Port<object>()
            };

            class JoinBeta
            {
                public string Message;
                public string Category;

                public JoinBeta()
                {
                }

                public JoinBeta(object[] args)
                {
                    Message = args[0].ToString();
                    Category = args[1].ToString();
                }
            }

            ccr.Port<object>[] _joinBetaPorts = new ccr.Port<object>[2]{
                new ccr.Port<object>(),
                new ccr.Port<object>()
            };
        }

        #endregion

        #region OnVEXBumper_2UpdateHandler class

        class OnVEXBumper_2UpdateHandler : HandlerBase
        {
            ///////////////////////////////////////////////////////////////////
            // if0 if{__use__2.snippet0.snippet.noop - [(__use__2.snippet0.snippet.expr1 - __use__2.snippet0.snippet.join)(__use__2.snippet0.snippet.expr0 - &__use__2.snippet0.snippet.join)(__use__2.snippet0.snippet.expr - &__use__2.snippet0.snippet.join)] - __use__2.snippet0.call - __use__2.snippet0.call.iftype}else{}
            // element0
            // __use__12.snippet.snippet.noop - [(__use__12.snippet.snippet.expr0 - __use__12.snippet.snippet.join)(__use__12.snippet.snippet.expr - &__use__12.snippet.snippet.join)] - __use__12.snippet.call - __use__12.snippet.call.iftype
            // element
            // __use__22.snippet0.snippet.noop - [(__use__22.snippet0.snippet.expr0 - __use__22.snippet0.snippet.join)(__use__22.snippet0.snippet.expr - &__use__22.snippet0.snippet.join)] - __use__22.snippet0.call - __use__22.snippet0.call.iftype
            ///////////////////////////////////////////////////////////////////

            public OnVEXBumper_2UpdateHandler(DriveTestDiagramService service, ccr.DispatcherQueue queue)
                : base(service, queue)
            {

                // Activate merge handlers
                Activate(ccr.Arbiter.Receive(true, _mergeAlpha, _mergeAlphaHandler));
                Activate(ccr.Arbiter.Receive(true, _mergeBeta, _mergeBetaHandler));
            }

            ///////////////////////////////////////////////////////////////////
            // if0 if{__use__2.snippet0.snippet.noop - [(__use__2.snippet0.snippet.expr1 - __use__2.snippet0.snippet.join)(__use__2.snippet0.snippet.expr0 - &__use__2.snippet0.snippet.join)(__use__2.snippet0.snippet.expr - &__use__2.snippet0.snippet.join)] - __use__2.snippet0.call - __use__2.snippet0.call.iftype}else{}
            ///////////////////////////////////////////////////////////////////

            public IEnumerator<ccr.ITask> RunHandler(Microsoft.Robotics.Services.ContactSensor.Proxy.Update message)
            {
                Increment();

                if (message.Body.Pressed)
                {
                    drive.DriveDistanceRequest request = new drive.DriveDistanceRequest();
                    request.DriveDistanceStage = Microsoft.Robotics.Services.Drive.Proxy.DriveStage.InitialRequest;
                    request.Power = 0.1D;
                    request.Distance = 1D;

                    Increment();
                    Activate(
                        ccr.Arbiter.Choice(
                            VEXDrivePort.DriveDistance(request),
                            OnDriveDistanceSuccess,
                            OnDriveDistanceFault
                        )
                    );
                }

                Decrement();

                yield return WaitUntilComplete();
            }

            void OnDriveDistanceSuccess(dssp.DefaultUpdateResponseType response)
            {

                Increment();
                _mergeAlpha.Post(response);

                Decrement();
            }

            void OnDriveDistanceFault(soap.Fault response)
            {

                Increment();
                _mergeBeta.Post(response);

                Decrement();
            }

            ccr.Port<Microsoft.Dss.ServiceModel.Dssp.DefaultUpdateResponseType> _mergeAlpha = new ccr.Port<Microsoft.Dss.ServiceModel.Dssp.DefaultUpdateResponseType>();

            ///////////////////////////////////////////////////////////////////
            // __use__12.snippet.snippet.noop - [(__use__12.snippet.snippet.expr0 - __use__12.snippet.snippet.join)(__use__12.snippet.snippet.expr - &__use__12.snippet.snippet.join)] - __use__12.snippet.call - __use__12.snippet.call.iftype
            ///////////////////////////////////////////////////////////////////

            void _mergeAlphaHandler(Microsoft.Dss.ServiceModel.Dssp.DefaultUpdateResponseType message)
            {
                log.LogInfoRequest request = new log.LogInfoRequest();
                request.Category = Stringize(message);
                request.Message = Stringize(message);
                LogPort.LogInfo(request);

                Decrement();
            }

            ccr.Port<W3C.Soap.Fault> _mergeBeta = new ccr.Port<W3C.Soap.Fault>();

            ///////////////////////////////////////////////////////////////////
            // __use__22.snippet0.snippet.noop - [(__use__22.snippet0.snippet.expr0 - __use__22.snippet0.snippet.join)(__use__22.snippet0.snippet.expr - &__use__22.snippet0.snippet.join)] - __use__22.snippet0.call - __use__22.snippet0.call.iftype
            ///////////////////////////////////////////////////////////////////

            void _mergeBetaHandler(W3C.Soap.Fault message)
            {
                log.LogInfoRequest request = new log.LogInfoRequest();
                request.Category = Stringize(message.Code);
                request.Message = message.Reason[0].Value;
                LogPort.LogInfo(request);

                Decrement();
            }

            class JoinAlpha
            {
                public string Distance;
                public string Power;
                public string DriveDistanceStage;

                public JoinAlpha()
                {
                }

                public JoinAlpha(object[] args)
                {
                    Distance = args[0].ToString();
                    Power = args[1].ToString();
                    DriveDistanceStage = args[2].ToString();
                }
            }

            ccr.Port<object>[] _joinAlphaPorts = new ccr.Port<object>[3]{
                new ccr.Port<object>(),
                new ccr.Port<object>(),
                new ccr.Port<object>()
            };

            class JoinBeta
            {
                public string Message;
                public string Category;

                public JoinBeta()
                {
                }

                public JoinBeta(object[] args)
                {
                    Message = args[0].ToString();
                    Category = args[1].ToString();
                }
            }

            ccr.Port<object>[] _joinBetaPorts = new ccr.Port<object>[2]{
                new ccr.Port<object>(),
                new ccr.Port<object>()
            };

            class JoinGamma
            {
                public string Message;
                public string Category;

                public JoinGamma()
                {
                }

                public JoinGamma(object[] args)
                {
                    Message = args[0].ToString();
                    Category = args[1].ToString();
                }
            }

            ccr.Port<object>[] _joinGammaPorts = new ccr.Port<object>[2]{
                new ccr.Port<object>(),
                new ccr.Port<object>()
            };
        }

        #endregion

        #region OnVEXBumper_3UpdateHandler class

        class OnVEXBumper_3UpdateHandler : HandlerBase
        {
            ///////////////////////////////////////////////////////////////////
            // if1 if{__use__4.snippet0.snippet.noop - [(__use__4.snippet0.snippet.expr1 - __use__4.snippet0.snippet.join)(__use__4.snippet0.snippet.expr0 - &__use__4.snippet0.snippet.join)(__use__4.snippet0.snippet.expr - &__use__4.snippet0.snippet.join)] - __use__4.snippet0.call - __use__4.snippet0.call.iftype}else{}
            // element0
            // __use__12.snippet.snippet.noop - [(__use__12.snippet.snippet.expr0 - __use__12.snippet.snippet.join)(__use__12.snippet.snippet.expr - &__use__12.snippet.snippet.join)] - __use__12.snippet.call - __use__12.snippet.call.iftype
            // element
            // __use__22.snippet0.snippet.noop - [(__use__22.snippet0.snippet.expr0 - __use__22.snippet0.snippet.join)(__use__22.snippet0.snippet.expr - &__use__22.snippet0.snippet.join)] - __use__22.snippet0.call - __use__22.snippet0.call.iftype
            ///////////////////////////////////////////////////////////////////

            public OnVEXBumper_3UpdateHandler(DriveTestDiagramService service, ccr.DispatcherQueue queue)
                : base(service, queue)
            {

                // Activate merge handlers
                Activate(ccr.Arbiter.Receive(true, _mergeAlpha, _mergeAlphaHandler));
                Activate(ccr.Arbiter.Receive(true, _mergeBeta, _mergeBetaHandler));
            }

            ///////////////////////////////////////////////////////////////////
            // if1 if{__use__4.snippet0.snippet.noop - [(__use__4.snippet0.snippet.expr1 - __use__4.snippet0.snippet.join)(__use__4.snippet0.snippet.expr0 - &__use__4.snippet0.snippet.join)(__use__4.snippet0.snippet.expr - &__use__4.snippet0.snippet.join)] - __use__4.snippet0.call - __use__4.snippet0.call.iftype}else{}
            ///////////////////////////////////////////////////////////////////

            public IEnumerator<ccr.ITask> RunHandler(Microsoft.Robotics.Services.ContactSensor.Proxy.Update message)
            {
                Increment();

                if (message.Body.Pressed)
                {
                    drive.RotateDegreesRequest request = new drive.RotateDegreesRequest();
                    request.Power = 0.2D;
                    request.Degrees = (double)30;

                    Increment();
                    Activate(
                        ccr.Arbiter.Choice(
                            VEXDrivePort.RotateDegrees(request),
                            OnRotateDegreesSuccess,
                            OnRotateDegreesFault
                        )
                    );
                }

                Decrement();

                yield return WaitUntilComplete();
            }

            void OnRotateDegreesSuccess(dssp.DefaultUpdateResponseType response)
            {

                Increment();
                _mergeAlpha.Post(response);

                Decrement();
            }

            void OnRotateDegreesFault(soap.Fault response)
            {

                Increment();
                _mergeBeta.Post(response);

                Decrement();
            }

            ccr.Port<Microsoft.Dss.ServiceModel.Dssp.DefaultUpdateResponseType> _mergeAlpha = new ccr.Port<Microsoft.Dss.ServiceModel.Dssp.DefaultUpdateResponseType>();

            ///////////////////////////////////////////////////////////////////
            // __use__12.snippet.snippet.noop - [(__use__12.snippet.snippet.expr0 - __use__12.snippet.snippet.join)(__use__12.snippet.snippet.expr - &__use__12.snippet.snippet.join)] - __use__12.snippet.call - __use__12.snippet.call.iftype
            ///////////////////////////////////////////////////////////////////

            void _mergeAlphaHandler(Microsoft.Dss.ServiceModel.Dssp.DefaultUpdateResponseType message)
            {
                log.LogInfoRequest request = new log.LogInfoRequest();
                request.Category = Stringize(message);
                request.Message = Stringize(message);
                LogPort.LogInfo(request);

                Decrement();
            }

            ccr.Port<W3C.Soap.Fault> _mergeBeta = new ccr.Port<W3C.Soap.Fault>();

            ///////////////////////////////////////////////////////////////////
            // __use__22.snippet0.snippet.noop - [(__use__22.snippet0.snippet.expr0 - __use__22.snippet0.snippet.join)(__use__22.snippet0.snippet.expr - &__use__22.snippet0.snippet.join)] - __use__22.snippet0.call - __use__22.snippet0.call.iftype
            ///////////////////////////////////////////////////////////////////

            void _mergeBetaHandler(W3C.Soap.Fault message)
            {
                log.LogInfoRequest request = new log.LogInfoRequest();
                request.Category = Stringize(message.Code);
                request.Message = message.Reason[0].Value;
                LogPort.LogInfo(request);

                Decrement();
            }

            class JoinAlpha
            {
                public string Degrees;
                public string Power;
                public string RotateDegreesStage;

                public JoinAlpha()
                {
                }

                public JoinAlpha(object[] args)
                {
                    Degrees = args[0].ToString();
                    Power = args[1].ToString();
                    RotateDegreesStage = args[2].ToString();
                }
            }

            ccr.Port<object>[] _joinAlphaPorts = new ccr.Port<object>[3]{
                new ccr.Port<object>(),
                new ccr.Port<object>(),
                new ccr.Port<object>()
            };

            class JoinBeta
            {
                public string Message;
                public string Category;

                public JoinBeta()
                {
                }

                public JoinBeta(object[] args)
                {
                    Message = args[0].ToString();
                    Category = args[1].ToString();
                }
            }

            ccr.Port<object>[] _joinBetaPorts = new ccr.Port<object>[2]{
                new ccr.Port<object>(),
                new ccr.Port<object>()
            };

            class JoinGamma
            {
                public string Message;
                public string Category;

                public JoinGamma()
                {
                }

                public JoinGamma(object[] args)
                {
                    Message = args[0].ToString();
                    Category = args[1].ToString();
                }
            }

            ccr.Port<object>[] _joinGammaPorts = new ccr.Port<object>[2]{
                new ccr.Port<object>(),
                new ccr.Port<object>()
            };
        }

        #endregion

        #region OnVEXBumper_4UpdateHandler class

        class OnVEXBumper_4UpdateHandler : HandlerBase
        {
            ///////////////////////////////////////////////////////////////////
            // if2 if{__use__6.snippet0.snippet.noop - [(__use__6.snippet0.snippet.expr0 - __use__6.snippet0.snippet.join)(__use__6.snippet0.snippet.expr - &__use__6.snippet0.snippet.join)] - __use__6.snippet0.call - __use__6.snippet0.call.iftype}else{}
            // element0
            // __use__12.snippet.snippet.noop - [(__use__12.snippet.snippet.expr0 - __use__12.snippet.snippet.join)(__use__12.snippet.snippet.expr - &__use__12.snippet.snippet.join)] - __use__12.snippet.call - __use__12.snippet.call.iftype
            // element
            // __use__22.snippet0.snippet.noop - [(__use__22.snippet0.snippet.expr0 - __use__22.snippet0.snippet.join)(__use__22.snippet0.snippet.expr - &__use__22.snippet0.snippet.join)] - __use__22.snippet0.call - __use__22.snippet0.call.iftype
            ///////////////////////////////////////////////////////////////////

            public OnVEXBumper_4UpdateHandler(DriveTestDiagramService service, ccr.DispatcherQueue queue)
                : base(service, queue)
            {

                // Activate merge handlers
                Activate(ccr.Arbiter.Receive(true, _mergeAlpha, _mergeAlphaHandler));
                Activate(ccr.Arbiter.Receive(true, _mergeBeta, _mergeBetaHandler));
            }

            ///////////////////////////////////////////////////////////////////
            // if2 if{__use__6.snippet0.snippet.noop - [(__use__6.snippet0.snippet.expr0 - __use__6.snippet0.snippet.join)(__use__6.snippet0.snippet.expr - &__use__6.snippet0.snippet.join)] - __use__6.snippet0.call - __use__6.snippet0.call.iftype}else{}
            ///////////////////////////////////////////////////////////////////

            public IEnumerator<ccr.ITask> RunHandler(Microsoft.Robotics.Services.ContactSensor.Proxy.Update message)
            {
                Increment();

                if (message.Body.Pressed)
                {
                    drive.SetDrivePowerRequest request = new drive.SetDrivePowerRequest();
                    request.RightWheelPower = 0.4D;
                    request.LeftWheelPower = 0.3D;

                    Increment();
                    Activate(
                        ccr.Arbiter.Choice(
                            VEXDrivePort.SetDrivePower(request),
                            OnSetDrivePowerSuccess,
                            OnSetDrivePowerFault
                        )
                    );
                }

                Decrement();

                yield return WaitUntilComplete();
            }

            void OnSetDrivePowerSuccess(dssp.DefaultUpdateResponseType response)
            {

                Increment();
                _mergeAlpha.Post(response);

                Decrement();
            }

            void OnSetDrivePowerFault(soap.Fault response)
            {

                Increment();
                _mergeBeta.Post(response);

                Decrement();
            }

            ccr.Port<Microsoft.Dss.ServiceModel.Dssp.DefaultUpdateResponseType> _mergeAlpha = new ccr.Port<Microsoft.Dss.ServiceModel.Dssp.DefaultUpdateResponseType>();

            ///////////////////////////////////////////////////////////////////
            // __use__12.snippet.snippet.noop - [(__use__12.snippet.snippet.expr0 - __use__12.snippet.snippet.join)(__use__12.snippet.snippet.expr - &__use__12.snippet.snippet.join)] - __use__12.snippet.call - __use__12.snippet.call.iftype
            ///////////////////////////////////////////////////////////////////

            void _mergeAlphaHandler(Microsoft.Dss.ServiceModel.Dssp.DefaultUpdateResponseType message)
            {
                log.LogInfoRequest request = new log.LogInfoRequest();
                request.Category = Stringize(message);
                request.Message = Stringize(message);
                LogPort.LogInfo(request);

                Decrement();
            }

            ccr.Port<W3C.Soap.Fault> _mergeBeta = new ccr.Port<W3C.Soap.Fault>();

            ///////////////////////////////////////////////////////////////////
            // __use__22.snippet0.snippet.noop - [(__use__22.snippet0.snippet.expr0 - __use__22.snippet0.snippet.join)(__use__22.snippet0.snippet.expr - &__use__22.snippet0.snippet.join)] - __use__22.snippet0.call - __use__22.snippet0.call.iftype
            ///////////////////////////////////////////////////////////////////

            void _mergeBetaHandler(W3C.Soap.Fault message)
            {
                log.LogInfoRequest request = new log.LogInfoRequest();
                request.Category = Stringize(message.Code);
                request.Message = message.Reason[0].Value;
                LogPort.LogInfo(request);

                Decrement();
            }

            class JoinAlpha
            {
                public string LeftWheelPower;
                public string RightWheelPower;

                public JoinAlpha()
                {
                }

                public JoinAlpha(object[] args)
                {
                    LeftWheelPower = args[0].ToString();
                    RightWheelPower = args[1].ToString();
                }
            }

            ccr.Port<object>[] _joinAlphaPorts = new ccr.Port<object>[2]{
                new ccr.Port<object>(),
                new ccr.Port<object>()
            };

            class JoinBeta
            {
                public string Message;
                public string Category;

                public JoinBeta()
                {
                }

                public JoinBeta(object[] args)
                {
                    Message = args[0].ToString();
                    Category = args[1].ToString();
                }
            }

            ccr.Port<object>[] _joinBetaPorts = new ccr.Port<object>[2]{
                new ccr.Port<object>(),
                new ccr.Port<object>()
            };

            class JoinGamma
            {
                public string Message;
                public string Category;

                public JoinGamma()
                {
                }

                public JoinGamma(object[] args)
                {
                    Message = args[0].ToString();
                    Category = args[1].ToString();
                }
            }

            ccr.Port<object>[] _joinGammaPorts = new ccr.Port<object>[2]{
                new ccr.Port<object>(),
                new ccr.Port<object>()
            };
        }

        #endregion

        #region OnVEXBumper_5UpdateHandler class

        class OnVEXBumper_5UpdateHandler : HandlerBase
        {
            ///////////////////////////////////////////////////////////////////
            // if3 if{__use__8.snippet0.snippet.noop - [(__use__8.snippet0.snippet.expr0 - __use__8.snippet0.snippet.join)(__use__8.snippet0.snippet.expr - &__use__8.snippet0.snippet.join)] - __use__8.snippet0.call - __use__8.snippet0.call.iftype}else{}
            // element0
            // __use__12.snippet.snippet.noop - [(__use__12.snippet.snippet.expr0 - __use__12.snippet.snippet.join)(__use__12.snippet.snippet.expr - &__use__12.snippet.snippet.join)] - __use__12.snippet.call - __use__12.snippet.call.iftype
            // element
            // __use__22.snippet0.snippet.noop - [(__use__22.snippet0.snippet.expr0 - __use__22.snippet0.snippet.join)(__use__22.snippet0.snippet.expr - &__use__22.snippet0.snippet.join)] - __use__22.snippet0.call - __use__22.snippet0.call.iftype
            ///////////////////////////////////////////////////////////////////

            public OnVEXBumper_5UpdateHandler(DriveTestDiagramService service, ccr.DispatcherQueue queue)
                : base(service, queue)
            {

                // Activate merge handlers
                Activate(ccr.Arbiter.Receive(true, _mergeAlpha, _mergeAlphaHandler));
                Activate(ccr.Arbiter.Receive(true, _mergeBeta, _mergeBetaHandler));
            }

            ///////////////////////////////////////////////////////////////////
            // if3 if{__use__8.snippet0.snippet.noop - [(__use__8.snippet0.snippet.expr0 - __use__8.snippet0.snippet.join)(__use__8.snippet0.snippet.expr - &__use__8.snippet0.snippet.join)] - __use__8.snippet0.call - __use__8.snippet0.call.iftype}else{}
            ///////////////////////////////////////////////////////////////////

            public IEnumerator<ccr.ITask> RunHandler(Microsoft.Robotics.Services.ContactSensor.Proxy.Update message)
            {
                Increment();

                if (message.Body.Pressed)
                {
                    drive.SetDriveSpeedRequest request = new drive.SetDriveSpeedRequest();
                    request.RightWheelSpeed = 0.6D;
                    request.LeftWheelSpeed = 0.5D;

                    Increment();
                    Activate(
                        ccr.Arbiter.Choice(
                            VEXDrivePort.SetDriveSpeed(request),
                            OnSetDriveSpeedSuccess,
                            OnSetDriveSpeedFault
                        )
                    );
                }

                Decrement();

                yield return WaitUntilComplete();
            }

            void OnSetDriveSpeedSuccess(dssp.DefaultUpdateResponseType response)
            {

                Increment();
                _mergeAlpha.Post(response);

                Decrement();
            }

            void OnSetDriveSpeedFault(soap.Fault response)
            {

                Increment();
                _mergeBeta.Post(response);

                Decrement();
            }

            ccr.Port<Microsoft.Dss.ServiceModel.Dssp.DefaultUpdateResponseType> _mergeAlpha = new ccr.Port<Microsoft.Dss.ServiceModel.Dssp.DefaultUpdateResponseType>();

            ///////////////////////////////////////////////////////////////////
            // __use__12.snippet.snippet.noop - [(__use__12.snippet.snippet.expr0 - __use__12.snippet.snippet.join)(__use__12.snippet.snippet.expr - &__use__12.snippet.snippet.join)] - __use__12.snippet.call - __use__12.snippet.call.iftype
            ///////////////////////////////////////////////////////////////////

            void _mergeAlphaHandler(Microsoft.Dss.ServiceModel.Dssp.DefaultUpdateResponseType message)
            {
                log.LogInfoRequest request = new log.LogInfoRequest();
                request.Category = Stringize(message);
                request.Message = Stringize(message);
                LogPort.LogInfo(request);

                Decrement();
            }

            ccr.Port<W3C.Soap.Fault> _mergeBeta = new ccr.Port<W3C.Soap.Fault>();

            ///////////////////////////////////////////////////////////////////
            // __use__22.snippet0.snippet.noop - [(__use__22.snippet0.snippet.expr0 - __use__22.snippet0.snippet.join)(__use__22.snippet0.snippet.expr - &__use__22.snippet0.snippet.join)] - __use__22.snippet0.call - __use__22.snippet0.call.iftype
            ///////////////////////////////////////////////////////////////////

            void _mergeBetaHandler(W3C.Soap.Fault message)
            {
                log.LogInfoRequest request = new log.LogInfoRequest();
                request.Category = Stringize(message.Code);
                request.Message = message.Reason[0].Value;
                LogPort.LogInfo(request);

                Decrement();
            }

            class JoinAlpha
            {
                public string LeftWheelSpeed;
                public string RightWheelSpeed;

                public JoinAlpha()
                {
                }

                public JoinAlpha(object[] args)
                {
                    LeftWheelSpeed = args[0].ToString();
                    RightWheelSpeed = args[1].ToString();
                }
            }

            ccr.Port<object>[] _joinAlphaPorts = new ccr.Port<object>[2]{
                new ccr.Port<object>(),
                new ccr.Port<object>()
            };

            class JoinBeta
            {
                public string Message;
                public string Category;

                public JoinBeta()
                {
                }

                public JoinBeta(object[] args)
                {
                    Message = args[0].ToString();
                    Category = args[1].ToString();
                }
            }

            ccr.Port<object>[] _joinBetaPorts = new ccr.Port<object>[2]{
                new ccr.Port<object>(),
                new ccr.Port<object>()
            };

            class JoinGamma
            {
                public string Message;
                public string Category;

                public JoinGamma()
                {
                }

                public JoinGamma(object[] args)
                {
                    Message = args[0].ToString();
                    Category = args[1].ToString();
                }
            }

            ccr.Port<object>[] _joinGammaPorts = new ccr.Port<object>[2]{
                new ccr.Port<object>(),
                new ccr.Port<object>()
            };
        }

        #endregion

        #region OnVEXBumper_6UpdateHandler class

        class OnVEXBumper_6UpdateHandler : HandlerBase
        {
            ///////////////////////////////////////////////////////////////////
            // if4 if{[(__use__20.snippet0.snippet.noop - __use__20.snippet0.snippet.expr - __use__20.snippet0.snippet.join)] - __use__20.snippet0.call - __use__20.snippet0.call.iftype}else{[(__use__21.snippet0.snippet.noop - __use__21.snippet0.snippet.expr - __use__21.snippet0.snippet.join)] - __use__21.snippet0.call - __use__21.snippet0.call.iftype}
            // &element0
            // &element
            // element0
            // __use__12.snippet.snippet.noop - [(__use__12.snippet.snippet.expr0 - __use__12.snippet.snippet.join)(__use__12.snippet.snippet.expr - &__use__12.snippet.snippet.join)] - __use__12.snippet.call - __use__12.snippet.call.iftype
            // element
            // __use__22.snippet0.snippet.noop - [(__use__22.snippet0.snippet.expr0 - __use__22.snippet0.snippet.join)(__use__22.snippet0.snippet.expr - &__use__22.snippet0.snippet.join)] - __use__22.snippet0.call - __use__22.snippet0.call.iftype
            ///////////////////////////////////////////////////////////////////

            public OnVEXBumper_6UpdateHandler(DriveTestDiagramService service, ccr.DispatcherQueue queue)
                : base(service, queue)
            {

                // Activate merge handlers
                Activate(ccr.Arbiter.Receive(true, _mergeAlpha, _mergeAlphaHandler));
                Activate(ccr.Arbiter.Receive(true, _mergeBeta, _mergeBetaHandler));
            }

            ///////////////////////////////////////////////////////////////////
            // if4 if{[(__use__20.snippet0.snippet.noop - __use__20.snippet0.snippet.expr - __use__20.snippet0.snippet.join)] - __use__20.snippet0.call - __use__20.snippet0.call.iftype}else{[(__use__21.snippet0.snippet.noop - __use__21.snippet0.snippet.expr - __use__21.snippet0.snippet.join)] - __use__21.snippet0.call - __use__21.snippet0.call.iftype}
            ///////////////////////////////////////////////////////////////////

            public IEnumerator<ccr.ITask> RunHandler(Microsoft.Robotics.Services.ContactSensor.Proxy.Update message)
            {
                Increment();

                if (message.Body.Pressed)
                {
                    drive.EnableDriveRequest request = new drive.EnableDriveRequest();
                    request.Enable = false;

                    Increment();
                    Activate(
                        ccr.Arbiter.Choice(
                            VEXDrivePort.EnableDrive(request),
                            OnEnableDriveSuccess,
                            OnEnableDriveFault
                        )
                    );
                }
                else
                {
                    drive.EnableDriveRequest requestA = new drive.EnableDriveRequest();
                    requestA.Enable = true;

                    Increment();
                    Activate(
                        ccr.Arbiter.Choice(
                            VEXDrivePort.EnableDrive(requestA),
                            OnEnableDrive1Success,
                            OnEnableDrive1Fault
                        )
                    );
                }

                Decrement();

                yield return WaitUntilComplete();
            }

            void OnEnableDriveSuccess(dssp.DefaultUpdateResponseType response)
            {

                Increment();
                _mergeAlpha.Post(response);

                Decrement();
            }

            void OnEnableDriveFault(soap.Fault response)
            {

                Increment();
                _mergeBeta.Post(response);

                Decrement();
            }

            void OnEnableDrive1Success(dssp.DefaultUpdateResponseType response)
            {

                Increment();
                _mergeAlpha.Post(response);

                Decrement();
            }

            void OnEnableDrive1Fault(soap.Fault response)
            {

                Increment();
                _mergeBeta.Post(response);

                Decrement();
            }

            ccr.Port<Microsoft.Dss.ServiceModel.Dssp.DefaultUpdateResponseType> _mergeAlpha = new ccr.Port<Microsoft.Dss.ServiceModel.Dssp.DefaultUpdateResponseType>();

            ///////////////////////////////////////////////////////////////////
            // __use__12.snippet.snippet.noop - [(__use__12.snippet.snippet.expr0 - __use__12.snippet.snippet.join)(__use__12.snippet.snippet.expr - &__use__12.snippet.snippet.join)] - __use__12.snippet.call - __use__12.snippet.call.iftype
            ///////////////////////////////////////////////////////////////////

            void _mergeAlphaHandler(Microsoft.Dss.ServiceModel.Dssp.DefaultUpdateResponseType message)
            {
                log.LogInfoRequest request = new log.LogInfoRequest();
                request.Category = Stringize(message);
                request.Message = Stringize(message);
                LogPort.LogInfo(request);

                Decrement();
            }

            ccr.Port<W3C.Soap.Fault> _mergeBeta = new ccr.Port<W3C.Soap.Fault>();

            ///////////////////////////////////////////////////////////////////
            // __use__22.snippet0.snippet.noop - [(__use__22.snippet0.snippet.expr0 - __use__22.snippet0.snippet.join)(__use__22.snippet0.snippet.expr - &__use__22.snippet0.snippet.join)] - __use__22.snippet0.call - __use__22.snippet0.call.iftype
            ///////////////////////////////////////////////////////////////////

            void _mergeBetaHandler(W3C.Soap.Fault message)
            {
                log.LogInfoRequest request = new log.LogInfoRequest();
                request.Category = Stringize(message.Code);
                request.Message = message.Reason[0].Value;
                LogPort.LogInfo(request);

                Decrement();
            }

            class JoinAlpha
            {
                public string Enable;

                public JoinAlpha()
                {
                }

                public JoinAlpha(object[] args)
                {
                    Enable = args[0].ToString();
                }
            }

            ccr.Port<object>[] _joinAlphaPorts = new ccr.Port<object>[1]{
                new ccr.Port<object>()
            };

            class JoinBeta
            {
                public string Message;
                public string Category;

                public JoinBeta()
                {
                }

                public JoinBeta(object[] args)
                {
                    Message = args[0].ToString();
                    Category = args[1].ToString();
                }
            }

            ccr.Port<object>[] _joinBetaPorts = new ccr.Port<object>[2]{
                new ccr.Port<object>(),
                new ccr.Port<object>()
            };

            class JoinGamma
            {
                public string Message;
                public string Category;

                public JoinGamma()
                {
                }

                public JoinGamma(object[] args)
                {
                    Message = args[0].ToString();
                    Category = args[1].ToString();
                }
            }

            ccr.Port<object>[] _joinGammaPorts = new ccr.Port<object>[2]{
                new ccr.Port<object>(),
                new ccr.Port<object>()
            };

            ccr.Port<object>[] _secondJoinAlphaPorts = new ccr.Port<object>[1]{
                new ccr.Port<object>()
            };
        }

        #endregion

        #region Handler utility base class

        class HandlerBase : ccr.CcrServiceBase
        {
            ccr.Port<ccr.EmptyValue> _complete = new ccr.Port<ccr.EmptyValue>();
            ccr.Port<ccr.EmptyValue> _shutdown = new ccr.Port<ccr.EmptyValue>();
            List<ccr.Port<object>[]> _joins = new List<ccr.Port<object>[]>();
            DriveTestDiagramService _service;
            int _count;

            public HandlerBase(DriveTestDiagramService service, ccr.DispatcherQueue queue)
                : base(queue)
            {
                _service = service;
            }

            protected DriveTestDiagramState State
            {
                get { return _service._state; }
            }

            bool _stateChanged;

            public bool StateChanged
            {
                get { return _stateChanged; }
                set { _stateChanged = value; }
            }

            protected log.LogOperations LogPort
            {
                get { return _service._logPort; }
            }

            protected contactsensor.ContactSensorArrayOperations VEXBumper_1Port
            {
                get { return _service._vEXBumper_1Port; }
            }

            protected contactsensor.ContactSensorArrayOperations VEXBumper_2Port
            {
                get { return _service._vEXBumper_2Port; }
            }

            protected contactsensor.ContactSensorArrayOperations VEXBumper_3Port
            {
                get { return _service._vEXBumper_3Port; }
            }

            protected contactsensor.ContactSensorArrayOperations VEXBumper_4Port
            {
                get { return _service._vEXBumper_4Port; }
            }

            protected contactsensor.ContactSensorArrayOperations VEXBumper_5Port
            {
                get { return _service._vEXBumper_5Port; }
            }

            protected contactsensor.ContactSensorArrayOperations VEXBumper_6Port
            {
                get { return _service._vEXBumper_6Port; }
            }

            protected drive.DriveOperations VEXDrivePort
            {
                get { return _service._vEXDrivePort; }
            }

            public ccr.Port<ccr.EmptyValue> Complete
            {
                get { return _complete; }
            }

            protected void Increment()
            {
                System.Threading.Interlocked.Increment(ref _count);
            }

            protected void Decrement()
            {
                int offset = 0;

                foreach (ccr.Port<object>[] join in _joins)
                {
                    bool complete = true;
                    int joinCount = 0;

                    foreach (ccr.Port<object> sink in join)
                    {
                        if (sink.ItemCount != 0)
                        {
                            joinCount += sink.ItemCount;
                        }
                        else
                        {
                            complete = false;
                        }
                    }
                    if (complete == false)
                    {
                        offset += joinCount;
                    }
                }

                if (System.Threading.Interlocked.Decrement(ref _count) <= offset)
                {
                    _shutdown.Post(ccr.EmptyValue.SharedInstance);
                    _complete.Post(ccr.EmptyValue.SharedInstance);
                }
            }

            protected void Decrement(int count)
            {
                for(int i = 0; i < count; i++)
                {
                    Decrement();
                }
            }

            protected void RegisterJoin(ccr.Port<object>[] join)
            {
                _joins.Add(join);
            }

            protected ccr.ITask WaitUntilComplete()
            {
                return ccr.Arbiter.Receive(false, _shutdown, NullDelegate);
            }

            protected static void NullDelegate(ccr.EmptyValue token)
            {
            }

            protected void SendNotification<T>(object notification)
                where T : dssp.DsspOperation, new()
            {
                _service.SendNotification<T>(_service._subMgr, notification);
            }

            protected void FaultHandler(soap.Fault fault, string msg)
            {
                _service.LogError(null, msg, fault);
            }

            protected static string Stringize(object obj)
            {
                if (obj == null)
                {
                    return string.Empty;
                }
                else
                {
                    return obj.ToString();
                }
            }

            protected void UnhandledResponse(W3C.Soap.Fault fault)
            {
                _service.LogError("Unhandled fault response from partner service", fault);
                Decrement();
            }

            protected void UnhandledResponse<T>(T response)
            {
                Decrement();
            }

            #region Type Cast Functions

            protected static bool BoolCast(object obj)
            {
                if (obj != null && obj is IConvertible)
                {
                    try
                    {
                        return ((IConvertible)obj).ToBoolean(System.Globalization.CultureInfo.InvariantCulture);
                    }
                    catch (FormatException)
                    {
                    }
                }
                return false;
            }

            protected static byte ByteCast(object obj)
            {
                if (obj != null && obj is IConvertible)
                {
                    try
                    {
                        return ((IConvertible)obj).ToByte(System.Globalization.CultureInfo.InvariantCulture);
                    }
                    catch (FormatException)
                    {
                    }
                }
                return 0;
            }

            protected static char CharCast(object obj)
            {
                if (obj != null && obj is IConvertible)
                {
                    try
                    {
                        return ((IConvertible)obj).ToChar(System.Globalization.CultureInfo.InvariantCulture);
                    }
                    catch (FormatException)
                    {
                    }
                }
                return '\0';
            }

            protected static decimal DecimalCast(object obj)
            {
                if (obj != null && obj is IConvertible)
                {
                    try
                    {
                        return ((IConvertible)obj).ToDecimal(System.Globalization.CultureInfo.InvariantCulture);
                    }
                    catch (FormatException)
                    {
                    }
                }
                return 0;
            }

            protected static double DoubleCast(object obj)
            {
                if (obj != null && obj is IConvertible)
                {
                    try
                    {
                        return ((IConvertible)obj).ToDouble(System.Globalization.CultureInfo.InvariantCulture);
                    }
                    catch (FormatException)
                    {
                    }
                }
                return 0;
            }

            protected static float FloatCast(object obj)
            {
                if (obj != null && obj is IConvertible)
                {
                    try
                    {
                        return ((IConvertible)obj).ToSingle(System.Globalization.CultureInfo.InvariantCulture);
                    }
                    catch (FormatException)
                    {
                    }
                }
                return 0;
            }

            protected static int IntCast(object obj)
            {
                if (obj != null && obj is IConvertible)
                {
                    try
                    {
                        return ((IConvertible)obj).ToInt32(System.Globalization.CultureInfo.InvariantCulture);
                    }
                    catch (FormatException)
                    {
                    }
                }
                return 0;
            }

            protected static long LongCast(object obj)
            {
                if (obj != null && obj is IConvertible)
                {
                    try
                    {
                        return ((IConvertible)obj).ToInt64(System.Globalization.CultureInfo.InvariantCulture);
                    }
                    catch (FormatException)
                    {
                    }
                }
                return 0;
            }

            protected static sbyte SByteCast(object obj)
            {
                if (obj != null && obj is IConvertible)
                {
                    try
                    {
                        return ((IConvertible)obj).ToSByte(System.Globalization.CultureInfo.InvariantCulture);
                    }
                    catch (FormatException)
                    {
                    }
                }
                return 0;
            }

            protected static short ShortCast(object obj)
            {
                if (obj != null && obj is IConvertible)
                {
                    try
                    {
                        return ((IConvertible)obj).ToInt16(System.Globalization.CultureInfo.InvariantCulture);
                    }
                    catch (FormatException)
                    {
                    }
                }
                return 0;
            }

            protected static uint UIntCast(object obj)
            {
                if (obj != null && obj is IConvertible)
                {
                    try
                    {
                        return ((IConvertible)obj).ToUInt32(System.Globalization.CultureInfo.InvariantCulture);
                    }
                    catch (FormatException)
                    {
                    }
                }
                return 0;
            }

            protected static ulong ULongCast(object obj)
            {
                if (obj != null && obj is IConvertible)
                {
                    try
                    {
                        return ((IConvertible)obj).ToUInt64(System.Globalization.CultureInfo.InvariantCulture);
                    }
                    catch (FormatException)
                    {
                    }
                }
                return 0;
            }

            protected static ushort UShortCast(object obj)
            {
                if (obj != null && obj is IConvertible)
                {
                    try
                    {
                        return ((IConvertible)obj).ToUInt16(System.Globalization.CultureInfo.InvariantCulture);
                    }
                    catch (FormatException)
                    {
                    }
                }
                return 0;
            }

            #endregion

            #region List Functions

            protected List<T> ListAdd<T>(List<T> list, T item)
            {
                List<T> output = new List<T>(list);
                output.Add(item);
                return output;
            }

            protected List<T> ListConcat<T>(List<T> head, List<T> tail)
            {
                List<T> output = new List<T>(head);
                output.AddRange(tail);
                return output;
            }

            protected int ListIndex<T>(List<T> list, T item)
            {
                return list.IndexOf(item);
            }

            protected List<T> ListRemove<T>(List<T> list, int index)
            {
                List<T> output = new List<T>(list);
                output.RemoveAt(index);
                return output;
            }

            protected List<T> ListReverse<T>(List<T> list)
            {
                List<T> output = new List<T>(list);
                output.Reverse();
                return output;
            }

            protected List<T> ListSort<T>(List<T> list)
            {
                List<T> output = new List<T>(list);
                output.Sort();
                return output;
            }

            protected List<T> ListInsert<T>(List<T> list, T item, int index)
            {
                List<T> output = new List<T>(list);
                output.Insert(index, item);
                return output;
            }

            #endregion
        }

        #endregion
    }
}
